package com.cskaoyan.controller.admin;

import com.cskaoyan.bean.*;
import com.cskaoyan.service.StorageService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

/**
 * Description
 * @author Qiu
 * @date Created on 2021/10/18
 */
@RestController
@RequestMapping("/admin/storage")
public class StorageController {

    @Autowired
    StorageService storageService;

    /**
     * 对象存储分页
     * @param key
     * @param name
     * @param param
     * @return
     */
    @RequiresPermissions("admin:storage:list")
    @GetMapping("/list")
    public BaseRespVo list(String key, String name, BaseParam param){

        BasePageData<Storage> data = storageService.list(key, name, param);

        return BaseRespVo.ok(data);
    }

    /**
     * 图片上传功能
     * @param req
     * @param file
     * @return
     */
    @RequiresPermissions("admin:storage:create")
    @RequestMapping("/create")
    public BaseRespVo create(HttpServletRequest req, MultipartFile file)  {
        Storage storage = storageService.create(req, file);
        return BaseRespVo.ok(storage);
    }


    /**
     * 删除存储对象
     * @param storage
     * @return
     */
    @RequiresPermissions("admin:storage:delete")
    @PostMapping("/delete")
    public BaseRespVo delete(@RequestBody Storage storage) {

        storageService.delete(storage);
        return BaseRespVo.ok();
    }

    /**
     * 修改存储对象
     * @param storage
     * @return
     */
    @RequiresPermissions("admin:storage:update")
    @PostMapping("/update")
    public BaseRespVo update(@RequestBody Storage storage) {

        Storage newStorage = storageService.update(storage);
        return BaseRespVo.ok(newStorage);
    }
}
